package org.liquidByte.trafficMustang.androidApp.listview.row;

import java.security.InvalidParameterException;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.Bindings;
import org.liquidByte.trafficMustang.androidApp.listview.adapter.RowAdapter;
import org.liquidByte.trafficMustang.androidApp.services.dto.DeslocamentoDTO;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class DeslocamentoRow extends ModelListViewRow<DeslocamentoDTO> implements OnClickListener {
	
	private TextView txtSituacao;
	private TextView txtTaxa;
	private ImageButton btnFinalizar;
	private ImageButton btnCancelar;
	
	private RowActionListener<DeslocamentoRow> finalizarActionListener;
	private RowActionListener<DeslocamentoRow> cancelarActionListener;
	
	public DeslocamentoRow(RowAdapter<DeslocamentoDTO> parentAdapter, View view) {
		super(parentAdapter, view);
		if (view.getId() != R.layout.listview_row_deslocamento) {
			throw new InvalidParameterException("view is not a type of listview_row_deslocamento");
		}
	}
	
	public TextView getTxtSituacao() {
		if (this.txtSituacao == null) {
			this.txtSituacao = (TextView) this.getView().findViewById(
					R.id.row_deslocamento_txtSituacao);
		}
		return txtSituacao;
	}

	public TextView getTxtTaxa() {
		if (this.txtTaxa == null) {
			this.txtTaxa = (TextView) this.getView().findViewById(
					R.id.row_deslocamento_txtTaxa);
		}
		return this.txtTaxa;
	}	

	public ImageButton getBtnFinalizar() {
		if (this.btnFinalizar == null) {
			this.btnFinalizar = (ImageButton)this.getView().findViewById(R.id.row_deslocamento_finalizar);
		}
		return btnFinalizar;
	}


	public ImageButton getBtnCancelar() {
		if (this.btnCancelar == null) {
			this.btnCancelar = (ImageButton)this.getView().findViewById(R.id.row_deslocamento_cancelar);
		}
		return btnCancelar;
	}
	
	public void setFinalizarActionListener(RowActionListener<DeslocamentoRow> finalizarActionListener) {
		this.finalizarActionListener = finalizarActionListener;
	}

	public void setCancelarActionListener(RowActionListener<DeslocamentoRow> cancelarActionListener) {
		this.cancelarActionListener = cancelarActionListener;
	}

	@Override
	public void bindToView() {
		DeslocamentoDTO deslocamento = (DeslocamentoDTO) this.getModel();
		if (deslocamento != null) {
			this.getTxtSituacao().setText(deslocamento.getSituacao());
			Bindings.currencySet(this.getTxtTaxa(), deslocamento.getTaxa());
		} else {
			this.getTxtSituacao().setText(null);
			this.getTxtTaxa().setText(null);
		}
	}


	@Override
	public void init() {
		this.getBtnCancelar().setOnClickListener(this);
		this.getBtnFinalizar().setOnClickListener(this);
	}
	
	@Override
	public void onClick(View view) {
		if (this.getBtnCancelar() == view) {
			if (this.cancelarActionListener != null) {
				this.cancelarActionListener.actionPerformed(this);
			}
		} else if (this.getBtnFinalizar() == view) {
			if (this.finalizarActionListener != null) {
				this.finalizarActionListener.actionPerformed(this);
			}
		}		
	}
}
